package com.vf.s.mvc.scene.controller;

import java.util.List;

import com.jfinal.kit.StrKit;
import com.vf.core.controller.BaseController;
import com.vf.core.util.UuidUtil;
import com.vf.s.common.model.biz.BizMap;
import com.vf.s.common.model.biz.BizSceneMap;
public class BizSceneMapContoller  extends BaseController{
	
	public void setting() {
		BizSceneMap bizSceneMap=BizSceneMap.dao.findById(this.getPara("mapId"));
		if(bizSceneMap!=null) {
			this.setAttr("bizSceneMap",BizSceneMap.dao.findById(this.getPara("mapId")));
			this.setAttr("map",BizMap.dao.findById(bizSceneMap.getMapId()));
		}
		render("setting.html");
	}
	
	public void select() {
		this.setAttr("layerId",getPara("layerId"));
		this.setAttr("maps",BizMap.dao.find("SELECT A.* FROM "+BizMap.TABLE_NAME+" A "));
		render("select.html");
	}
	
	public void save() {
		BizSceneMap model = getModel(BizSceneMap.class);
		BizSceneMap bizSceneMap=BizSceneMap.dao.findFirst("SELECT * FROM "+BizSceneMap.TABLE_NAME+" WHERE MAPID=? AND LAYERID=? ",model.getMapId(),model.getLayerId());
		if(bizSceneMap!=null) {
			if (model.update()) {
				renderSuccess("更新成功！", model);
			} else {
				renderError("更新失败！");
			}
		}else {
			model.setId(UuidUtil.getUUID());
			if (model.save()) {
				renderSuccess("保存成功！", model);
			} else {
				renderError("保存失败！");
			}
		}
	}
	
	public void delete(){
		String id = getPara("id");
		String mapId = getPara("mapId");
		String layerId = getPara("layerId");
		if(StrKit.isBlank(id)) {
			BizSceneMap bizSceneMap=BizSceneMap.dao.findFirst("SELECT * FROM "+BizSceneMap.TABLE_NAME+" WHERE MAPID=? AND LAYERID=? ",mapId,layerId);
			if(bizSceneMap!=null) {
				if(bizSceneMap.delete()) {
					renderSuccess("删除成功！");
				}else {
					renderError("删除失败！");
				}
			}else {
				renderError("数据不存在！");
			}
		}else {
			BizSceneMap model=BizSceneMap.dao.findById(id);
			if(model!=null) {
				if(model.delete()) {
					renderSuccess("删除成功！");
				}else {
					renderError("删除失败！");
				}
			}else {
				renderError("数据不存在！");
			}
		}
	}
	
	public void getList() {
		String sceneId = getPara("sceneId");
		String layerId = getPara("layerId");
		String whereForMap="SELECT A.* FROM "+BizMap.TABLE_NAME+" A , "+BizSceneMap.TABLE_NAME+" B WHERE A.ID=B.MAPID AND B.SCENEID=? ";
		if(!StrKit.isBlank(layerId)) {
			whereForMap+=" AND B.LAYERID='"+layerId+"' ";
		}
		whereForMap+=" ORDER BY B.SORT ASC ";
		List<BizMap> maps=BizMap.dao.find(whereForMap,sceneId);
		
		if(maps!=null  && maps.size()>0) {
			this.renderSuccess("成功！", maps);
		}else {
			this.renderError("未查询到底图资源！");
		}
	}
	
	public void getById() {
		String layerId = getPara("id");
		BizMap bizMap=BizMap.dao.findFirst("select A.*,B.SORT from "+BizMap.TABLE_NAME+" A,"+BizSceneMap.TABLE_NAME+" B where A.ID=B.MAPID AND B.id=? ",layerId);
		if(bizMap!=null) {
			this.renderSuccess("成功！", bizMap);
		}else {
			renderError("数据不存在!");
		}
	}
}
